﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ControlsLib.Toolbars;
using GestionePolveriLib.Facades;
using System.Windows.Forms;
using GestionePolveriLib.BL;
using FrameworkLib.Utilities;
using System.Collections;

namespace GestionePolveri2
{
    public class MainToolBarDecorator
    {
        public event EventHandler LoadComplete;

        private MainFormToolbar m_toolbar;
        private bool[] m_eventManagement = null;

        ToolStripComboBox cmb = null;
        ToolStripComboBox cmb1 = null;
        ToolStripComboBox cmb2 = null;

        public MainToolBarDecorator(MainFormToolbar toolbar)
        {
            m_toolbar = toolbar;

            m_toolbar.Items.Clear();

            m_eventManagement = new bool[3] { false, false, false };
        }

        public void AddComboTurni()
        {
            cmb = m_toolbar.CreaCombo("cmbTurni", "Turni");

            //GestionePolveriLib.Utilities.FilterComboBoxHelper.SetupTurniComboBox(cmb.ComboBox);

            //m_eventManagement[0] = true;

            //StartEventManagement();

            TurniFacade tf = new TurniFacade();
            tf.LoadTurniAsync((result) =>
            {
                if (cmb != null)
                {
                    cmb.ComboBox.LoadData("Descrizione", "ID", result, new Turno() { Id = -1, Descrizione = "Seleziona..." });
                }

                m_eventManagement[0] = true;

                StartEventManagement();
            });
        }

        public void AddComboImpianti()
        {
            cmb1 = m_toolbar.CreaCombo("cmbImp", "Impianti");

            //GestionePolveriLib.Utilities.FilterComboBoxHelper.SetupImpiantiComboBox(cmb1.ComboBox);

            //m_eventManagement[1] = true;

            //StartEventManagement();

            ImpiantoFacade pf = new ImpiantoFacade();
            pf.RunAsync<List<Impianto>>(() => pf.GetImpianti(false), (result) =>
            {
                cmb1.ComboBox.LoadData("Nome", "ID", result, new Impianto() { ID = -1, Nome = "Seleziona...", Fittizio = false });

                m_eventManagement[1] = true;

                StartEventManagement();
            });
        }

        public void AddComboImpiantiFittizi()
        {
            cmb2 = m_toolbar.CreaCombo("cmbImpFitt", "Impianti Fitt.");

            //GestionePolveriLib.Utilities.FilterComboBoxHelper.SetupImpiantiFittiziComboBox(cmb2.ComboBox);

            //m_eventManagement[2] = true;

            //StartEventManagement();

            ImpiantoFacade tf = new ImpiantoFacade();
            tf.RunAsync<List<Impianto>>(() => tf.GetImpianti(true), (result) =>
            {
                cmb2.ComboBox.LoadData("Nome", "ID", result, new Impianto() { ID = -1, Nome = "Seleziona...", Fittizio = true });

                m_eventManagement[2] = true;

                StartEventManagement();
            });
        }

        void cmb_SelectedIndexChanged(object sender, EventArgs e)
        {
            m_toolbar.OnEventRaised(sender);
        }

        private void StartEventManagement()
        {
            foreach (bool b in m_eventManagement)
            {
                if (!b) return;
            }

            cmb.SelectedIndexChanged += new EventHandler(cmb_SelectedIndexChanged);
            cmb1.SelectedIndexChanged += new EventHandler(cmb_SelectedIndexChanged);
            cmb2.SelectedIndexChanged += new EventHandler(cmb_SelectedIndexChanged);

            if (LoadComplete != null)
            {
                LoadComplete(this, EventArgs.Empty);
            }
        }

        public Turno Turno
        {
            get { return (Turno)((ToolStripComboBox)m_toolbar.Items["cmbTurni"]).ComboBox.SelectedItem; }
        }
    }
}
